package cn.zhuama.user.service.impl;

import cn.zhuama.basic.content.Content;
import cn.zhuama.basic.util.Md5Utils;
import cn.zhuama.basic.util.StrUtils;
import cn.zhuama.exception.businessException;
import cn.zhuama.user.domain.Logininfo;
import cn.zhuama.user.domain.User;
import cn.zhuama.user.dto.RegisterDto;
import cn.zhuama.user.mapper.LogininfoMapper;
import cn.zhuama.user.mapper.UserMapper;
import cn.zhuama.user.service.IUserService;
import cn.zhuama.basic.service.impl.BaseServiceImpl;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void phoneRegister(RegisterDto dto) {
        String phone = dto.getPhone();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        String smsCode = dto.getSmsCode();
        if(StringUtils.isEmpty(phone)||StringUtils.isEmpty(password)||StringUtils.isEmpty(passwordRepeat)){
            throw new businessException("电话号码或者密码或者短信验证码不能为空");
        }
        User user = userMapper.selectByPhone(phone);
        if(user!=null){
            throw new businessException("手机号已经注册了");

        }
        Object redisSmsCode = redisTemplate.opsForValue().get(Content.PHONE_REGISTER_PREFIX+phone);
        System.out.println(redisSmsCode);
        if(redisSmsCode==null){
            throw  new businessException("短信验证码过期");
        }
        if(!smsCode.equalsIgnoreCase(redisSmsCode.toString().split(":")[0])){
            throw  new businessException("短信验证码输入错误");
        }
        if(redisSmsCode==null){
            throw  new businessException("两次密码不一致");
        }

        /*3.业务实现*/
        user = initUser(phone,password);

        Logininfo logininfo = initLogininfo(user);
        logininfoMapper.insert(logininfo);



        user.setLogininfoId(logininfo.getId());
        userMapper.insert(user);
    }

    private Logininfo initLogininfo(User user) {
        Logininfo logininfo = new Logininfo();

        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(Logininfo.WEB_USER);
        logininfo.setDisable(user.getState()==1?true:false);
        return logininfo;
    }

    private User initUser(String phone, String password) {
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String md5pwd = Md5Utils.encrypByMd5(password + salt);
        user.setPassword(md5pwd);
        user.setState(User.enable);
        return user;
    }


}

